package cn.xy.base.server.bean;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author auto
 */
public class IPage {

    /**
     * 分页大小
     */
    @ApiModelProperty(value = "分页大小", dataType = "Long")
    private Long size;

    /**
     * 当前页
     */
    @ApiModelProperty(value = "当前页", dataType = "Long")
    private Long current;

    /**
     * 排序字段
     */
    @ApiModelProperty(value = "排序字段", dataType = "String")
    private String sortColumn;

    /**
     * 是否正序排列，默认 true
     */
    @ApiModelProperty(value = "是否正序排列，默认 true", dataType = "Boolean")
    private Boolean sortAsc;

    public IPage() {
    }

    public IPage(Long size, Long current) {
        this.size = size;
        this.current = current;
    }

    @ApiIgnore
    public <T> Page<T> toPage() {
        Page<T> page = new Page<>();
        page.setSize(this.size != null ? this.size : 10);
        page.setCurrent(this.current != null ? this.current : 1);
        page.setOptimizeCountSql(true);
        if (CharSequenceUtil.isNotEmpty(sortColumn)) {
            sortAsc = sortAsc == null || sortAsc;
            sortColumn = CharSequenceUtil.toUnderlineCase(sortColumn);
            page.setOrders(CollUtil.toList(new OrderItem(sortColumn, sortAsc)));
        }
        return page;
    }

    public Long getSize() {
        return this.size != null ? this.size : 10;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public Long getCurrent() {
        return this.current != null ? this.current : 1;
    }

    public void setCurrent(Long current) {
        this.current = current;
    }

    public String getSortColumn() {
        return sortColumn;
    }

    public void setSortColumn(String sortColumn) {
        this.sortColumn = sortColumn;
    }

    public Boolean getSortAsc() {
        return sortAsc;
    }

    public void setSortAsc(Boolean sortAsc) {
        this.sortAsc = sortAsc;
    }
}
